#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 分组设置：
// 进入距离起始：100米，结束：900米，分组间隔：50米
#define GROUP_MIN      0
#define GROUP_MAX      850
#define GROUP_INTERVAL 50
#define GROUP_COUNT    (((GROUP_MAX - GROUP_MIN) / GROUP_INTERVAL) + 1)  // 18

// 统计数据（例如针对来向雷达）
static int up_enter_static[GROUP_COUNT] = {0};               // 各进入组的车辆数
static int up_group_static[GROUP_COUNT][GROUP_COUNT] = {0};  // 各进入组的消失组车辆数
static int up_total_enter = 0;

// 针对去向雷达的数据（如果需要，可类似定义）
static int down_enter_static[GROUP_COUNT] = {0};
static int down_group_static[GROUP_COUNT][GROUP_COUNT] = {0};
static int down_total_enter = 0;

int get_entry_group(int entering_distance, int8_t dev_type)
{
    if (dev_type == 0) {
        if (entering_distance < GROUP_MIN)
            entering_distance = GROUP_MIN;
        else if (entering_distance > GROUP_MAX)
            entering_distance = GROUP_MAX;

        return entering_distance / GROUP_INTERVAL;
    }
    else {
        if (entering_distance < GROUP_MIN || entering_distance > GROUP_MAX)
            return -1;
        return entering_distance / GROUP_INTERVAL;
    }
}

int get_exit_index(int leaving_distance, int8_t dev_type)
{
    if (dev_type == 0) {
        if (leaving_distance < GROUP_MIN)
            leaving_distance = GROUP_MIN;
        else if (leaving_distance > GROUP_MAX)
            leaving_distance = GROUP_MAX;
        return leaving_distance / GROUP_INTERVAL;
    }
    else {
        if (leaving_distance < 0 || leaving_distance > GROUP_MAX)
            return -1;
        return leaving_distance / GROUP_INTERVAL;
    }
}

void write_statistics_file(const char *filename, int8_t dev_type)
{
    FILE *fp = fopen(filename, "w");
    if (!fp) {
        perror("fopen");
        return;
    }

    if (dev_type == 0) {  // 来向雷达
        fprintf(fp, "总进入车辆数：%d\n", up_total_enter);
        for (int i = GROUP_COUNT - 1; i >= 0; i--) {
            if (up_enter_static[i] == 0)
                continue;
            int lower = GROUP_MIN + i * GROUP_INTERVAL;
            int upper = lower + GROUP_INTERVAL - 1;
            double ratio = up_total_enter > 0 ? (double)up_enter_static[i] / up_total_enter * 100 : 0;
            fprintf(fp, "进入距离为 %-3d-%-3d 米的车辆数：%5d                占比%0.1f%%\n",
                    lower, upper, up_enter_static[i], ratio);
            // 出口统计：假定出口数据只统计 9 个分组，对应 100~800米
            for (int j = GROUP_COUNT - 1; j >= 0; j--) {
                if (j > i)
                    continue;
                if (up_group_static[i][j] == 0)
                    continue;
                lower = GROUP_MIN + j * GROUP_INTERVAL;
                upper = lower + GROUP_INTERVAL - 1;
                double ratio2 = up_total_enter > 0 ? (double)up_group_static[i][j] / up_total_enter * 100 : 0;
                fprintf(fp, "       消失距离在 %-3d-%-3d 米的车辆数：%-5d         占比%0.1f%%\n",
                        lower, upper, up_group_static[i][j], ratio2);
            }
            fprintf(fp, "\n");
        }
    }
    else {
        fprintf(fp, "总进入车辆数：%d\n", down_total_enter);
        for (int i = 0; i < GROUP_COUNT; i++) {
            if (down_enter_static[i] == 0)
                continue;
            int lower = GROUP_MIN + i * GROUP_INTERVAL;
            int upper = lower + GROUP_INTERVAL - 1;
            double ratio = down_total_enter > 0 ? (double)down_enter_static[i] / down_total_enter * 100 : 0;
            fprintf(fp, "进入距离为 %-3d-%-3d 米的车辆数：%5d                占比%0.1f%%\n",
                    lower, upper, down_enter_static[i], ratio);
            for (int j = 0; j < GROUP_COUNT - 1; j++) {
                if (j < i)
                    continue;
                if (down_group_static[i][j] == 0)
                    continue;
                lower = GROUP_MIN + j * GROUP_INTERVAL;
                upper = lower + GROUP_INTERVAL - 1;
                double ratio2 = down_total_enter > 0 ? (double)down_group_static[i][j] / down_total_enter * 100 : 0;
                fprintf(fp, "       消失距离在 %-3d-%-3d 米的车辆数：%-5d         占比%0.1f%%\n",
                        lower, upper, down_group_static[i][j], ratio2);
            }
            fprintf(fp, "\n");
        }
    }
    fclose(fp);
}

/*
 * update_data: 根据新目标的进入距离和离开距离更新统计数据，并写入文件。
 * 对于 dev_type==0（来向雷达），如果进入距离 >= 离开距离，则更新；
 * 对于 dev_type!=0（去向雷达），如果进入距离 <= 离开距离，则更新。
 */
void update_data(int entering_distance, int leaving_distance, int8_t dev_type)
{
    if (dev_type == 0) {
        if (entering_distance < leaving_distance)
            return;
        int enter_group = get_entry_group(entering_distance, dev_type);
        int exit_index = get_exit_index(leaving_distance, dev_type);
        if (enter_group == -1 || exit_index == -1)
            return;
        up_enter_static[enter_group]++;
        up_total_enter++;
        up_group_static[enter_group][exit_index]++;
        write_statistics_file("统计来向雷达数据.txt", dev_type);
    }
    else {
        if (entering_distance > leaving_distance)
            return;
        int enter_group = get_entry_group(entering_distance, dev_type);
        int exit_index = get_exit_index(leaving_distance, dev_type);
        if (enter_group == -1 || exit_index == -1)
            return;
        down_enter_static[enter_group]++;
        down_total_enter++;
        down_group_static[enter_group][exit_index]++;
        write_statistics_file("统计去向雷达数据.txt", dev_type);
    }
}
